package tuse.components
{
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	
	import tuse.classes.ts_tuse;
	import tuse.components.tuseViewer.ts_tuseDisplay;

	public class ts_tuseView extends ts_component {
		private var _display	: ts_tuseDisplay;
		private var _moveTo		: Point;
		public function ts_tuseView(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0) {
			super(parent, ax, ay, awidth, aheight);
			var inner: Rectangle = getInner();
			_moveTo = new Point(0, 0);
//			setMoveTo(new Point(awidth / 2, 0));
			_display = new ts_tuseDisplay(this, inner.x, inner.y, inner.width, inner.height);
			addEvent(MouseEvent.MOUSE_MOVE, doMove, stage);
			addEvent(Event.ENTER_FRAME, doFrame);
			resetPerspective();
		}
		
		public function get PPj(): PerspectiveProjection {
			return transform.perspectiveProjection;
		}
		
		public function set PPj(a_pp: PerspectiveProjection): void {
			transform.perspectiveProjection = a_pp;
		}
		
		protected function resetPerspective(): void {
			var pp: PerspectiveProjection = new PerspectiveProjection();
//			pp.projectionCenter = new Point(width * 0.5, height * 0.70);
			PPj = pp;
		}
		
		public function getPPScale(a_z: Number): Number {
			return PPj.focalLength / (PPj.focalLength + a_z);
		}
		
		public function toSurface(surfOffset: Vector3D, displayPoint: Point, scale: Number=1): Vector3D {
			var mat: Matrix = new Matrix();
			
			mat.translate(-PPj.projectionCenter.x + display.x, -PPj.projectionCenter.y + display.y);
			mat.concat(display.view.getPPMat(surfOffset.z));
			mat.translate(PPj.projectionCenter.x - display.x, PPj.projectionCenter.y - display.y);
			mat.invert();
			
			mat.scale(scale, scale);
			mat.translate(-surfOffset.x * scale, -surfOffset.y * scale);
			
			var result: Point = mat.transformPoint(displayPoint); 
			
			return new Vector3D(result.x, result.y, surfOffset.z);
		}
		
		public function getPPMat(a_z: Number): Matrix {
			var scale: Number = PPj.focalLength / (PPj.focalLength + a_z);
			var matrix: Matrix = new Matrix();
			matrix.translate(-PPj.projectionCenter.x/scale, -PPj.projectionCenter.y/scale);
			matrix.scale(scale, scale);
			matrix.translate(PPj.projectionCenter.x, PPj.projectionCenter.y);
			return matrix;
		}
		
		public function get display(): ts_tuseDisplay {
			return _display;
		}
		
		public function assignTuse(a_tuse: ts_tuse): void {
			_display.assignTuse(a_tuse);
		}
		
		protected function setMoveTo(displayPos: Point): void {
			_moveTo.x = (-(displayPos.x + 40)) * 2;
			_moveTo.y = (height / 2 - displayPos.y) * 2;
		}
		
		protected function doMove(e: MouseEvent): void {
			setMoveTo(new Point(mouseX, mouseY));
		}
		
		protected function doFrame(e: Event): void {
			display.x += (_moveTo.x - display.x) / 20;
//			display.z += (_moveTo.y - display.z) / 10;
		}
	}
}